﻿<cfsilent>

    <cfset sql = "	SELECT
						s.sbj_id,s.sbj_name
					FROM
						t_subject s
					WHERE
						s.sbj_id = :subjectId "/>

	<cfset queryObj = new Query( datasource=application.dnsSlave ) />

	<cfset queryObj.addParam( name="subjectId", value=event.getArg('SubID'), cfsqltype="cf_sql_varchar" )/>

    <cfset rs_subjectEntity = queryObj.execute( sql=sql ).getResult()/>
	
	<cfset dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice") />

	<cfset sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice") />
	<cfset depId = sessionAdvice.getUserProp("teacherDepartment") />

	<cfset academicYear = event.getArg("AcademicYear") />
	<cfset academicTerm = event.getArg("AcademicTerm") />
	<cfset grade = event.getArg("Grade") />

	<cfset event.setArg("pageTitle", "教学运行基础数据 - 教学计划 - 专业学期开课计划 - " & rs_subjectEntity.sbj_name ) />

	<cfif len(academicYear) and len(academicTerm)>
		
        <cfset sql = "	SELECT
							t.term_id
						FROM
							t_term t
						WHERE
							t.academic_year = :academicYear 
							AND
							t.academic_term = :academicTerm " />
                            
		<cfset queryObj = new Query( datasource=application.dnsSlave ) />
        
		<cfset queryObj.addParam( name="academicYear", value=academicYear, cfsqltype="cf_sql_char" )/>
        <cfset queryObj.addParam( name="academicTerm", value=academicTerm, cfsqltype="cf_sql_char" )/>
        
        <cfset rs_termEntity = queryObj.execute( sql=sql ).getResult()/>

        <cfset sql = "	SELECT
							s.sbj_id
						FROM
							t_subject s
						WHERE
							s.sbj_id LIKE :condition "/>
		
        <cfset queryObj = new Query( datasource=application.dnsSlave ) />
        
		<cfset queryObj.addParam( name="condition", value=event.getArg('SubID') & '_', cfsqltype="cf_sql_varchar" )/>
        
        <cfset rs_dir = queryObj.execute( sql=sql ).getResult()/>
        
        <cfset sql = "	SELECT
							clz.grade,COUNT(DISTINCT p.cid) c_count
						FROM
							t_plan p
							INNER JOIN t_class clz ON clz.cls_id = p.cls_id
						WHERE
							clz.sbj_id = :subject 
							AND
							p.term_id = :term 
						GROUP BY
							clz.grade
						ORDER BY
							clz.grade DESC" />
        
        <cfset queryObj = new Query( datasource=application.dnsSlave ) />
        
		<cfset queryObj.addParam( name="subject", value = rs_subjectEntity.sbj_id, cfsqltype="cf_sql_varchar" )/>
        <cfset queryObj.addParam( name="term", value=rs_termEntity.term_id, cfsqltype="cf_sql_char" )/>
        
        <cfset rs_grade = queryObj.execute( sql=sql ).getResult()/>
        
		<cfif rs_grade.recordCount>
			<cfset currentGrade = rs_grade['grade'][1] />
		</cfif>
		
		<cfif len(grade)>
			<cfset currentGrade = grade />
		</cfif>
		
		
	</cfif>

	<cfset passArgs = structNew() />
	<cfset structInsert(passArgs, "TabID", "labDetail", true) />

	<cfset targetArgs = structNew() />

</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('planSchema', passArgs)#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">教学计划</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 info"></i><cfoutput>#rs_subjectEntity.sbj_name#</cfoutput> 专业学期开课计划
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<cfif len(academicYear) and len(academicTerm)>
			<!--- 活动学期数据正确 --->
			
			<li>
				
				<div class="UItab">
					<div class="tabLables">
                        <cfloop query="rs_grade">
							<span <cfif currentGrade eq rs_grade.grade>class="active"</cfif> id="labCourse<cfoutput>#rs_grade.grade#</cfoutput>" tabTarget="Course<cfoutput>#rs_grade.grade#</cfoutput>"><cfoutput>#rs_grade.grade#</cfoutput>级</span>
						</cfloop>
					</div>

					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
						
							/* init Tab */
							$("div.tabContent[@id!=' + $targetID + ']").hide();
							$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
							$("div.tabLables > span").click(function(){
								var $targetID = $(this).attr("tabTarget");
							
								$("div.tabLables span").removeClass("active");
								$(this).addClass("active");
							
								$("div.tabContent[@id!=' + $targetID + ']").hide();
								$("div.tabContent#" + $targetID).show();
							});
						
						});
						// -->
						//]]>
					</script>
				</div>

                <cfloop query="rs_grade">
                    
                    <cfset sql = "	SELECT
										p.cid,c.course_name,cp.prop_name,cn.cna_name,
										c.course_credit,i.institute_short,p.sbj_direction    
									FROM
										t_plan p
										INNER JOIN t_class clz ON p.cls_id = clz.cls_id
										INNER JOIN t_course c ON c.cid = p.cid
										INNER JOIN t_course_prop cp ON cp.prop_id = p.prop_id
										INNER JOIN t_course_nature cn ON cn.cna_id = p.cna_id
										INNER JOIN t_institute i ON i.institute_id = c.institute_id
									WHERE
										clz.sbj_id = :subject 
										AND
										p.term_id = :term 
										AND
										clz.grade = :grade 
									GROUP BY
										p.cid,c.course_name,cp.prop_name,cn.cna_name,c.course_credit,i.institute_short,p.sbj_direction
									ORDER BY
										p.sbj_direction,cp.prop_name,c.course_name" />
                    
                    <cfset queryObj = new Query( datasource=application.dnsSlave ) />
        
					<cfset queryObj.addParam( name="subject", value = rs_subjectEntity.sbj_id, cfsqltype="cf_sql_varchar" )/>
                    <cfset queryObj.addParam( name="term", value=rs_termEntity.term_id, cfsqltype="cf_sql_char" )/>
        			<cfset queryObj.addParam( name="grade", value=rs_grade.grade, cfsqltype="cf_sql_char" )/>
        
			        <cfset rs_course = queryObj.execute( sql=sql ).getResult()/>
                    
					<div id="Course<cfoutput>#rs_grade.grade#</cfoutput>" class="tabContent">
						
						<div class="noticeBlock">
							<p><span class="img icon16x16 message"></span><cfoutput>#rs_grade.grade#</cfoutput>级计划设课 <cfoutput>#rs_grade.c_count#</cfoutput> 门</p>
						</div>
						<cfset row = 0 />
						<table class="UIEditable">
							<thead>
								<tr>
									<td width="18"></td>
									<td width="48">类别</td>
									<td width="48">课程性质</td>
									<td>课程</td>
									<td width="24" align="right">学分</td>
									<td width="48">开课学院</td>
									<td>方向课</td>
									<td width="18"></td>
								</tr>
							</thead>
							<tbody>
								<cfloop query="rs_course">
									<cfset structInsert(targetArgs, "CID", rs_course.cid, true) />
									<cfset structInsert(targetArgs, "TID", rs_termEntity.term_id, true) />
									<cfset structInsert(targetArgs, "SubID", rs_subjectEntity.sbj_id, true) />

									<cfset row ++ />
									<tr class="editRows" rowid="<cfoutput>#rs_course.cid#</cfoutput>">
										<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
										<td><cfoutput>#rs_course.cna_name#</cfoutput></td>
										<td><cfoutput>#rs_course.prop_name#</cfoutput></td>
										<td><cfoutput>#rs_course.course_name#</cfoutput></td>
										<td><span class="numeric"><cfoutput>#numberFormat(rs_course.course_credit,'_._')#</cfoutput></span></td>
										<td><cfoutput>#rs_course.institute_short#</cfoutput></td>
										<cfif rs_course.sbj_direction neq "0">
											<cfset dirIds = [] />
											<cfloop array="#listToArray(rs_course.sbj_direction, '/')#" index="dir">
												<cfif dir neq "0">
													<cfset arrayAppend(dirIds, rs_subjectEntity.sbj_id & dir) />
												</cfif>
											</cfloop>

                                            <cfset sql = "	SELECT
																s.sbj_short
															FROM
																t_subject s
															WHERE
																s.sbj_id IN (:dirIds )" />
                                                
                                         	<cfset queryObj = new Query( datasource=application.dnsSlave ) />
        
											<cfset queryObj.addParam( name="dirIds", value = arrayToList(dirIds,","), cfsqltype="cf_sql_varchar", list=true )/>
                                    
                                            <cfset rs_direction = queryObj.execute( sql=sql ).getResult()/>

											<td><cfoutput>#valueList(rs_direction.sbj_short, " / ")#</cfoutput></td>
											<cfelse>
											<td></td>
										</cfif>
										<td><a href="<cfoutput>#buildURL('planSchemaClassByCourse', targetArgs)#</cfoutput>" class="stat info"></span></td>
									</tr>
								</cfloop>
							</tbody>
						</table>

					</div>
					
				</cfloop>

			</li>
			
			<cfelse>
			<!--- 要求设置一个数据处理学期 --->
			<li>
				<div class="clearfix" id="termSelector">
					
                    <cfset sql = " SELECT 
										a.term_id, a.academic_year, a.academic_term
									FROM t_term a 
									WHERE SUBSTR(a.academic_year,1,4) > :yearStart 
										  AND 
										  SUBSTR(a.academic_year,1,4) < :yearEnd 
									ORDER BY a.term_id"/>
					<cfset queryObj = new Query( datasource=application.dnsSlave ) />
					<cfset queryObj.addParam( name="yearStart", value=(year(now())-2), cfsqltype="cf_sql_char" )/>
                    <cfset queryObj.addParam( name="yearEnd", value=(year(now())+1), cfsqltype="cf_sql_char" )/>
                    <cfset rs_term= queryObj.execute( sql=sql ).getResult()/>
                    
					<div class="noticeBlock">
						<p><span class="img icon16x16 message"></span>请指定当前教学计划的学期</p>
						<ol>
							<cfloop query="rs_term">
								<li class="friendly"><span class="img icon10x10 execute"></span><a rowid="<cfoutput>#rs_term.term_id#</cfoutput>" href="javascript:void(0);"><cfoutput>#rs_term.academic_year#</cfoutput>-<cfoutput>#dictAdvice.getTermName(rs_term.academic_term)#</cfoutput></a></li>
							</cfloop>
						</ol>
					</div>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("a[rowid]").click(function(){
							
								var rowid = $(this).attr("rowid");
							
								$.post(
										"<cfoutput>#buildURL('apiTermPropChange')#</cfoutput>", 
										{
											'tid' : rowid,
											'prop' : "ForCourse",
											'status' : true,
											'seed': (new Date()).getTime()
										}, 
										function(data) {
										
											result = $.parseJSON(data);
										
											if (result.ForCourse) {
												window.location.reload(true);
											}
										
										});
							});
							
						});
						// -->
						//]]>
					</script>
					
				</div>
			</li>
		</cfif>		
	</ul>
</div>

